package com.zbbt.sportslottery.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.ActionCenter;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.ActionCenterAdapter;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;
import com.zbbt.sportslottery.util.LogUtil;

/**
 * 
 * @ClassName: ActionCenterFragment
 * @Description: 活动中心
 * @author：LiZhimin
 * @date：2014-12-12 下午4:35:41
 * @version V1.0
 */
public class ActionCenterFragment extends BaseMenuFragment {
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private ProgressBarCircularIndetermininate mProgressBar;
	private ListView mListView;
	private List<ActionCenter> dataList = new ArrayList<ActionCenter>();
	private ActionCenterAdapter adapter;
	private boolean hasMore = false;// 是否有下一页数据
	private int pageSize = 10;
	private int curPage = 1;
	private LinearLayout error_layout;// 无网络时 显示的界面
	private LinearLayout none_view_layout;// 无数据

	public ActionCenterFragment() {}

	public static ActionCenterFragment getInstance() {
		return new ActionCenterFragment();

	}

	@Override
	protected void initView(LayoutInflater inflater) {
		mView = inflater.inflate(R.layout.fragment_center, null);
		error_layout = (LinearLayout) findViewById(R.id.error_layout);
		error_layout.setVisibility(View.GONE);
		mListView = (ListView) findViewById(R.id.listview_center);
		none_view_layout = (LinearLayout) findViewById(R.id.none_view_layout);
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				curPage = 1;
				httpData(Constant.REFRESH);
				mSwipeRefreshLayout.setRefreshing(true);
			}
		});

		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBarCircularIndetermininate);
		adapter = new ActionCenterAdapter(context, dataList, handler);
		mListView.setAdapter(adapter);
		mProgressBar.setVisibility(View.VISIBLE);
		httpData(Constant.REFRESH);
	}

	@Override
	protected void setListener() {
		error_layout.setOnClickListener(this);
	}

	private void httpData(final int loadType) {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("curPage", curPage);
		jsonObject.addProperty("pageSize", pageSize);
		ZbHttpClient.postNewApi(ServerConfig.API_ACTION_LIST, jsonObject, new TypeGsonHttpHandler<SuperResult<ActionCenter>>() {
			@Override
			public void onSuccess(SuperResult<ActionCenter> data) {
				if (loadType == Constant.REFRESH) {
					dataList.clear();
				}
				if (data != null) {
					ArrayList<ActionCenter> resultList = data.getData();
					if (data.getTotalPage() == data.getCurPage()) {
						hasMore = false;
					} else {
						hasMore = true;
					}
					dataList.addAll(resultList);
				}
				adapter.notifyDataSetChanged();
				mProgressBar.setVisibility(View.GONE);
				mSwipeRefreshLayout.setRefreshing(false);
				error_layout.setVisibility(View.GONE);
				none_view_layout.setVisibility(View.GONE);
			}

			@Override
			public void onFailure(String errorCode, String errorMsg) {
				if (errorCode.equals(ServerConfig.HTTPERROR)) {
					error_layout.setVisibility(View.VISIBLE);
					none_view_layout.setVisibility(View.GONE);
				} else {
					dataList.clear();
					adapter.notifyDataSetChanged();
					error_layout.setVisibility(View.GONE);
					none_view_layout.setVisibility(View.VISIBLE);
				}
				hasMore = false;
				mProgressBar.setVisibility(View.GONE);
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.HAS_MORE:
				if (hasMore == true) {
					curPage++;
					httpData(Constant.HAS_MORE);
				}
				break;

			default:
				break;
			}
		}

	};

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.error_layout:
			curPage = 1;
			httpData(Constant.REFRESH);
			mProgressBar.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
	}

	@Override
	protected void initData() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.INTENT_ACTION_CHANGE_CITY);
		context.registerReceiver(broadcastReceiver, filter);
	}

	/**
	 * 注册的广播
	 */
	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (Constant.INTENT_ACTION_CHANGE_CITY.equals(intent.getAction())) {
				curPage = 1;
				dataList.clear();
				adapter.notifyDataSetChanged();
				httpData(Constant.REFRESH);
				mSwipeRefreshLayout.setRefreshing(true);
			}
		}

	};

}
